package com.prs.crm.report;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.UUID;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import com.prs.crm.domain.hr.Person;

public class ItextPdf extends JxlExcel {
	private float pageWidth;
	private float pageHeight;
	private float fontSize = 7f;
	private Color bgColor = new Color(240,240,240);
	private Color fontColor = Color.GRAY;
	private String title = "";
	private String standardID = "";
	private String register = "";
	private String registerTime = "";
	Color thcolor = new Color(173,212,249);
	Color bordercolor = new Color(100,100,100);
	
	public String getRegister() {
		return register;
	}

	public void setRegister(String register) {
		this.register = register;
	}

	public String getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(String registerTime) {
		this.registerTime = registerTime;
	}

	public String getTitle() {
		return title;
	}

	public String getStandardID() {
		return standardID;
	}

	public void setStandardID(String standardID) {
		this.standardID = standardID;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public float getPageWidth() {
		return pageWidth;
	}

	public void setPageWidth(float pageWidth) {
		this.pageWidth = pageWidth;
	}

	public float getPageHeight() {
		return pageHeight;
	}

	public void setPageHeight(float pageHeight) {
		this.pageHeight = pageHeight;
	}

	public float getFontSize() {
		return fontSize;
	}

	public void setFontSize(float fontSize) {
		this.fontSize = fontSize;
	}

	public Color getBgColor() {
		return bgColor;
	}

	public void setBgColor(Color bgColor) {
		this.bgColor = bgColor;
	}

	public Color getFontColor() {
		return fontColor;
	}

	public void setFontColor(Color fontColor) {
		this.fontColor = fontColor;
	}

	public ItextPdf(){}
	
	public ItextPdf(float pageWidth, float pageHeight, float fontSize,
			Color bgColor, Color fontColor) {
		super();
		this.pageWidth = pageWidth;
		this.pageHeight = pageHeight;
		this.fontSize = fontSize;
		this.bgColor = bgColor;
		this.fontColor = fontColor;
	}

	public Font setChinese(float fontsize){
		try {
			BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
			Font titleFont= new Font(bfChinese, getFontSize(), Font.NORMAL);
			return titleFont;
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public BaseFont getChineseBaseFont(){
		try {
			return BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * *这是一个自定义固定模版
	 * @param pageSize
	 * @param outPath
	 */
	public void outPdf(Rectangle pageSize, String outPath, List<String> items,List<Person> persons,Hashtable<?,?> hshData, boolean b){
		doc = new Document(pageSize);
		
		if(b){
			bgColor = Color.white;
			thcolor = Color.white;
			bordercolor = Color.black;
			fontColor = Color.black;
		}
		
		try {
			writer = PdfWriter.getInstance(doc, new FileOutputStream(outPath));
			doc.open();
			if(!"".equals(getTitle())){
				Paragraph para = new Paragraph(getTitle(),new Font(getChineseBaseFont(),10f,Font.BOLD));
				para.setIndentationLeft(-40f);
				para.setAlignment(Element.ALIGN_CENTER);
				doc.add(para);
			}
			/*
			Paragraph number = new Paragraph(new Chunk("标准编号 :   " + getStandardID(),new Font(getChineseBaseFont(),9f,Font.NORMAL)));
			doc.add(number);
			
			//draw line
			float pageHeight = doc.getPageSize().getHeight();
			float x = 81f;
			float y = pageHeight - 68f;
			drawLine(x, y);
			*/
			
			//file table data
			PdfPTable pt = getTableDataList(items, persons, hshData);
			pt.setSpacingBefore(18f);
			doc.add(pt);
			
			/*
			PdfPTable bt = new PdfPTable(2);
			bt.setWidthPercentage(100);
			PdfPCell pp = new PdfPCell();
			Chunk chun = new Chunk("登记人 :   " + getRegister(),new Font(getChineseBaseFont(),9f,Font.NORMAL));
			pp.addElement(chun);
			pp.setBorder(0);
			PdfPCell pp1 = new PdfPCell();
			pp1.addElement(new Chunk("登记时间 :   " + getRegisterTime(),new Font(getChineseBaseFont(),9f,Font.NORMAL)));
			pp1.setBorder(0);
			
			bt.addCell(pp);
			bt.addCell(pp1);
			bt.setSpacingBefore(5f);
			
			doc.add(bt);
			*/
			outOk();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public void drawLine(float x, float y) {
		PdfContentByte dc = writer.getDirectContent();
		dc.setLineWidth(0.1f);
		dc.setGrayStroke(0f);
		dc.moveTo(x, y);
		dc.lineTo(x + 200f, y);
		dc.stroke();
	}
	
	public PdfPTable getTableDataList(List<String> items,List<Person> persons,Hashtable<?,?> hshData){
		int rows = items.size();
		PdfPTable pt = new PdfPTable(rows);
		pt.setWidthPercentage(100);
		Font font = setChinese(7f);
		for(String item : items){
			pt.addCell(getPCell(item,font,thcolor, bordercolor));
		}
		
		for(int i = 0; i < persons.size(); i++){
			int t = 0;
			for(String item : items){
				if(t == 0) {
					pt.addCell(getPCell(persons.get(i).getResume().getName(),font,thcolor, bordercolor));
					t++;
				}else{
					String key = persons.get(i).getId() + "$" + item;
					String value = hshData.get(key) + "";
					pt.addCell(getPCell(value,font,thcolor, bordercolor));
				}
			}
		}
		return pt;
	}
	public static String getFileName(int type)
	{
		if (type == REPORTTYPE_PDF)
		{
			return UUID.randomUUID().toString().replace("-", "")+".pdf";
		}
		else if (type == REPORTTYPE_EXCEL)
		{
			return UUID.randomUUID().toString().replace("-", "")+".xls";
		}
		return null;
	}
	
	public PdfPTable getTableDataList(List<List<String>> dataList){
		int rows = dataList.get(0).size();
		PdfPTable pt = new PdfPTable(rows);
		pt.setWidthPercentage(100);
		Font font = setChinese(7f);
		
		
		for(int i = 0; i < dataList.size(); i++){
			if(i == 0){
				for(int j = 0; j < rows; j++){
					pt.addCell(getPCell(dataList.get(i).get(j),font,thcolor, bordercolor));
				}
			}else{
				for(int j = 0; j < rows; j++){
					if(i%2 == 0){
						pt.addCell(getPCell(dataList.get(i).get(j),font,bgColor, bordercolor));
					}else{
						pt.addCell(getPCell(dataList.get(i).get(j),font,Color.white, bordercolor));
					}
				}
			}
		}
		return pt;
	}
	
	public PdfPCell getPCell(String value, Font font, Color thcolor, Color bordercolor){
		PdfPCell pc = new PdfPCell();
		Paragraph chun = new Paragraph(value,font);
		pc.addElement(chun);
		pc.setUseAscender(true);
		pc.setVerticalAlignment(Cell.ALIGN_MIDDLE);
		pc.setBackgroundColor(thcolor);
		pc.setBorderColor(bordercolor);
		pc.setBorderWidth(0.1f);
		pc.setFixedHeight(15f);
		return pc;
	}
	
	public void outOk(){
		doc.close();
	}
	
	private PdfWriter writer;
	private Document doc;
	
	public static void main(String[] args) throws Exception {
		ItextPdf ipdf = new ItextPdf();
		ipdf.setTitle("客户档案表单");
		ipdf.setStandardID("0090036001460036");
		ipdf.setRegister("admin");
		ipdf.setRegisterTime("2009-12-03 17:35:33");
		
		
		List<List<String>> list = new ArrayList<List<String>>();
		
		
		
		
		for(int a=0; a<7; a++){
			List<String> astring = new ArrayList<String>();
			for(int i=0; i<10; i++){
				astring.add("这是 客户档案表单i客户档案表单is客户档案表单iss " + i + " : " + a +"row");
			}
			list.add(astring);
		}
		
//		ipdf.outPdf(new Rectangle(800,1000), "c:/test.pdf", list, true);
		
		Runtime.getRuntime().exec("cmd /c c:/test.pdf");
		
	}
	/**
	 * PDF
	 */
	public static final int REPORTTYPE_PDF = 1;
	
	/**
	 * EXCEL
	 */
	public static final int REPORTTYPE_EXCEL = 2;
	
	
	
	
	
}
